﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CsvHelper;

namespace Delta
{
    public partial class AllParameterCSVForm : Form
    {
        public Form1 form1;
        public AllParameterCSVForm(Form1 form)
        {
            InitializeComponent();
            form1 = form;
        }
        private bool ContainsChinese(string text)
        {
            return text.Any(c => c >= 0x4e00 && c <= 0x9fbb);
        }
        public DataTable LoadCSVWithCsvHelper(string filePath)
        {
            DataTable dt = new DataTable();

            try
            {
                using (var reader = new StreamReader(filePath, Encoding.UTF8))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {

                    // 读取CSV头部
                    csv.Read();
                    csv.ReadHeader();

                    // 创建列
                    foreach (string header in csv.HeaderRecord)
                    {
                        dt.Columns.Add(header);
                    }

                    // 读取数据行
                    while (csv.Read())
                    {
                        DataRow row = dt.NewRow();
                        foreach (DataColumn column in dt.Columns)
                        {
                            row[column.ColumnName] = csv.GetField(column.DataType, column.ColumnName);
                        }
                        dt.Rows.Add(row);
                    }
                }
              
            }
            catch (Exception ex)
            {
               

            }
            return dt;

        }
    
            private void SaveDataGridViewToCSV(DataGridView dataGridView, string filePath)
        {
            try
            {
                using (var writer = new StreamWriter(filePath, false, Encoding.UTF8))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    // 写入列标题
                    foreach (DataGridViewColumn column in dataGridView.Columns)
                    {
                        csv.WriteField(column.HeaderText);
                    }
                    csv.NextRecord();

                    // 写入数据行
                    foreach (DataGridViewRow row in dataGridView.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            foreach (DataGridViewCell cell in row.Cells)
                            {
                                csv.WriteField(cell.Value?.ToString());
                            }
                            csv.NextRecord();
                        }
                    }
                }

                MessageBox.Show("CSV文件保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存CSV文件时出错: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public object GetCellValueSafely(DataGridView dgv, int rowIndex, string columnName)
        {
            // 检查行索引是否有效
            if (rowIndex < 0 || rowIndex >= dgv.Rows.Count)
            {
                return null;
            }

            // 检查列是否存在
            if (!dgv.Columns.Contains(columnName))
            {
                return null;
            }

            // 检查是否是新建行
            if (dgv.Rows[rowIndex].IsNewRow)
            {
                return null;
            }

            // 返回单元格值
            return dgv.Rows[rowIndex].Cells[columnName].Value;
            //object value = GetCellValueSafely(dataGridView1, 2, "年龄");
            //if (value != null)
            //{
            //    // 处理获取到的值
            //}
        }

        private void btnParameterSet_Click(object sender, EventArgs e)
        {
            SaveDataGridViewToCSV(dataGridView1, Application.StartupPath + @"\ParameterCSV\Parameter.CSV");
        }
    }

}
